package com.wx.mall.goods.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.common.exception.BusinessException;
import com.wx.mall.goods.entity.Para;
import com.wx.mall.goods.mapper.ParaMapper;
import com.wx.mall.goods.service.IParaService;
import com.wx.mall.goods.vo.req.PageReqVO;
import com.wx.mall.goods.vo.resp.PageVO;
import com.wx.mall.goods.vo.resp.ParaSpecRespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 参数表 服务实现类
 * </p>
 *
 * @author LiRui
 * @since 2020-03-13
 */
@Transactional(propagation = Propagation.NESTED, isolation = Isolation.DEFAULT, readOnly = false, rollbackFor = BusinessException.class)
@Service
public class ParaServiceImpl extends ServiceImpl<ParaMapper, Para> implements IParaService {
    @Autowired
    private ParaMapper paraMapper;

    @Override
    public PageVO<ParaSpecRespVO> pageInfo(PageReqVO vo) {

        Integer pageSize = vo.getPageSize();
        int startIndex = (vo.getPageNum() - 1) * pageSize;
        List<ParaSpecRespVO> page = paraMapper.findPage(startIndex, pageSize);
        //查询总记录数
        Long rowCount = (long) paraMapper.getRowCount();
        //验证查询结果
        if (rowCount == 0) {
            throw new BusinessException(7001, "查询失败");
        }
        //封装数据
        PageVO<ParaSpecRespVO> pageVO = new PageVO<>();
        pageVO.setList(page);
        pageVO.setTotalRows(rowCount);
        pageVO.setPageNum(vo.getPageNum());
        pageVO.setPageSize(vo.getPageSize());
        return pageVO;
    }
}
